<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/13/0013
 * Time: 13:52
 */

namespace app\sports_admin\model;


use think\Model;
use think\Request;
use think\Session;
use traits\model\SoftDelete;

class Suggestion extends Model
{
    use SoftDelete;
    protected $pk = 'id';

    protected $table = 'suggestion';

    protected $autoWriteTimestamp = true;

    protected $hidden = ['update_time','user_id'];
    public function type()
    {
        return $this->belongsTo('suggestion_type','type_id','id')->field('id,title');
    }
    /**
     * 获取信息列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     */
    public static function getSuggestionInfo($page,$rows,$sort,$sortOrder,$start,$end)
    {
        $result = self::with('type')->where(function($query)use($start,$end){
            if($start && $end){
                $query->where('create_time','between',["{$start}","{$end}"]);
            }

        })->order("{$sort}","{$sortOrder}")->limit($rows)->page($page)->select();
        $count = self::with('type')->where(function($query)use($start,$end){
            if($start && $end){
                $query->where('create_time','between',["{$start}","{$end}"]);
            }
        })->count();
        return json(['rows'=>$result,'total'=>$count]);
    }
    //添加信息
    public static function suggestionAdd()
    {
        $data = Request::instance()->only("images,video,typeId,title,content,textarea");
        //$data['text'] = htmlspecialchars($data['text']);
        $name = Session::get('sport_admin.name').Session::get('sport_admin.id');
        return self::create([
            "name"=>$name,
            "images"=>$data['images'],
            "video"=>$data['video'],
            "text"=>htmlspecialchars($data['content']),
            "title"=>$data['title'],
            "introduce"=>$data['textarea'],
            "type_id"=>$data['typeId']
        ]);
    }
    //删除信息
    public static function suggestionDel()
    {
        $data = Request::instance()->only("id");
        return self::destroy($data['id']);
    }
}